<?php

namespace app\command;

use app\admin\model\Config;
use app\model\DistributionSetting;
use app\model\Live;
use app\model\Talent;
use app\service\DeepSeekService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use util\Logger;
use Wechat\CommerceApp;

class LiveTaskCommand extends Command
{
    protected function configure()
    {
        $this->setName('live')
            ->setDescription('获取达人直播列表');
    }

    protected function execute(Input $input, Output $output)
    {
        $mini_program_appid = Config::where('name', 'wx_xcx_appid')->value('value');
        $talent_list        = Talent::column('talent_appid, talent_nickname');
        $appids             = [];
        $ai_model           = new DeepSeekService();
        foreach ($talent_list as $talent_appid => $talent_nickname) {
            try {
                $save             = [];
                $sharer_info_list = CommerceApp::getInstance()->getLiveList($talent_appid, $mini_program_appid);
                if ($sharer_info_list) {
                    $appids[] = $talent_appid;
                    $db_live  = Live::where('appid', $talent_appid)->where('live_status', 1)->column('appid, id');
                    foreach ($sharer_info_list as $item) {
                        if (!isset($db_live[$talent_appid])) {
                            $messages = [
                                [
                                    'role'    => 'user',
                                    'content' => "微信达人名称：$talent_nickname\n帮我生成一个简略生动形象的分享文案，只生成一个，内容需要换行"
                                ]
                            ];
                            try {
                                $res        = $ai_model->createChatCompletion($messages);
                                $share_text = $res['content'];
                                $share_text = trim($share_text, '"');
                            } catch (\Exception $e) {
                                $share_text = '';
                            }
                            $save[] = [
                                'type'                => 1,
                                'appid'               => $talent_appid,
                                'export_id'           => $item['export_id'],
                                'description'         => $item['description'],
                                'promoter_share_link' => $item['promoter_share_link'],
                                'live_status'         => 1,
                                'share_text'          => $share_text,
                                'status'              => 1,
                            ];
                        } else {
                            $save[] = [
                                'id'                  => $db_live[$talent_appid],
                                'export_id'           => $item['export_id'],
                                'description'         => $item['description'],
                                'promoter_share_link' => $item['promoter_share_link'],
                                'live_status'         => 1,
                                'status'              => 1,
                            ];
                        }
                    }
                    if ($save) {
                        (new Live())->saveAll($save);
                    }
                } else {
                    Live::where('appid', $talent_appid)->update(['status' => 0]);
                }
            } catch (\Exception $e) {
//                $log = [
//                    'appid' => $talent_appid,
//                    'error' => $e->getMessage(),
//                    'file'  => $e->getFile(),
//                    'line'  => $e->getLine(),
//                ];
//                Logger::error('直播列表获取失败', $log);
                continue;
            }
        }
    }
}
